using System.Threading.Tasks;
using Matrix.Configuration.Events;

namespace Matrix.Configuration.Services
{
    /// <summary>
    /// 配置事件指标接口
    /// 负责收集和报告事件相关的性能指标
    /// </summary>
    public interface IConfigurationEventMetrics
    {
        /// <summary>
        /// 记录事件发布
        /// </summary>
        /// <param name="eventType">事件类型</param>
        /// <param name="importance">重要性级别</param>
        /// <param name="isLocal">是否为本地事件</param>
        void RecordEventPublished(string eventType, ConfigurationImportanceLevel importance, bool isLocal);

        /// <summary>
        /// 记录事件处理
        /// </summary>
        /// <param name="eventType">事件类型</param>
        /// <param name="success">是否成功</param>
        /// <param name="duration">处理耗时</param>
        /// <param name="isLocal">是否为本地事件</param>
        void RecordEventProcessed(string eventType, bool success, System.TimeSpan duration, bool isLocal);

        /// <summary>
        /// 记录事件重试
        /// </summary>
        /// <param name="eventType">事件类型</param>
        /// <param name="retryCount">重试次数</param>
        void RecordEventRetry(string eventType, int retryCount);

        /// <summary>
        /// 记录事件失败
        /// </summary>
        /// <param name="eventType">事件类型</param>
        /// <param name="errorType">错误类型</param>
        /// <param name="isLocal">是否为本地事件</param>
        void RecordEventFailure(string eventType, string errorType, bool isLocal);

        /// <summary>
        /// 记录配置缓存命中
        /// </summary>
        /// <param name="cacheType">缓存类型</param>
        /// <param name="hit">是否命中</param>
        void RecordCacheHit(string cacheType, bool hit);

        /// <summary>
        /// 记录配置验证
        /// </summary>
        /// <param name="validationType">验证类型</param>
        /// <param name="success">是否成功</param>
        /// <param name="duration">验证耗时</param>
        void RecordConfigurationValidation(string validationType, bool success, System.TimeSpan duration);

        /// <summary>
        /// 增加活跃事件计数
        /// </summary>
        /// <param name="eventType">事件类型</param>
        /// <param name="count">增量</param>
        void IncrementActiveEvents(string eventType, int count = 1);

        /// <summary>
        /// 减少活跃事件计数
        /// </summary>
        /// <param name="eventType">事件类型</param>
        /// <param name="count">减量</param>
        void DecrementActiveEvents(string eventType, int count = 1);

        /// <summary>
        /// 获取事件处理统计摘要
        /// </summary>
        /// <returns>事件统计摘要</returns>
        Task<EventMetricsSummary> GetMetricsSummaryAsync();

        /// <summary>
        /// 获取指定事件类型的指标
        /// </summary>
        /// <param name="eventType">事件类型</param>
        /// <returns>事件类型指标</returns>
        Task<EventTypeMetrics> GetEventTypeMetricsAsync(string eventType);

        /// <summary>
        /// 重置所有指标
        /// </summary>
        void ResetMetrics();
    }

    /// <summary>
    /// 事件指标摘要
    /// </summary>
    public class EventMetricsSummary
    {
        /// <summary>
        /// 总发布事件数
        /// </summary>
        public long TotalPublishedEvents { get; set; }

        /// <summary>
        /// 总处理事件数
        /// </summary>
        public long TotalProcessedEvents { get; set; }

        /// <summary>
        /// 总成功处理事件数
        /// </summary>
        public long TotalSuccessfulEvents { get; set; }

        /// <summary>
        /// 总失败事件数
        /// </summary>
        public long TotalFailedEvents { get; set; }

        /// <summary>
        /// 总重试次数
        /// </summary>
        public long TotalRetryCount { get; set; }

        /// <summary>
        /// 平均处理时间（毫秒）
        /// </summary>
        public double AverageProcessingTimeMs { get; set; }

        /// <summary>
        /// 成功率
        /// </summary>
        public double SuccessRate => TotalProcessedEvents > 0 ? (double)TotalSuccessfulEvents / TotalProcessedEvents : 0;

        /// <summary>
        /// 当前活跃事件数
        /// </summary>
        public long CurrentActiveEvents { get; set; }

        /// <summary>
        /// 本地事件数
        /// </summary>
        public long LocalEventCount { get; set; }

        /// <summary>
        /// 分布式事件数
        /// </summary>
        public long DistributedEventCount { get; set; }

        /// <summary>
        /// 按事件类型统计
        /// </summary>
        public System.Collections.Generic.Dictionary<string, EventTypeMetrics> EventTypeMetrics { get; set; } = new();

        /// <summary>
        /// 按重要性级别统计
        /// </summary>
        public System.Collections.Generic.Dictionary<ConfigurationImportanceLevel, long> EventsByImportance { get; set; } = new();

        /// <summary>
        /// 缓存命中率统计
        /// </summary>
        public System.Collections.Generic.Dictionary<string, double> CacheHitRates { get; set; } = new();

        /// <summary>
        /// 最后更新时间
        /// </summary>
        public System.DateTime LastUpdated { get; set; } = System.DateTime.UtcNow;
    }

    /// <summary>
    /// 事件类型指标
    /// </summary>
    public class EventTypeMetrics
    {
        /// <summary>
        /// 事件类型
        /// </summary>
        public string EventType { get; set; } = string.Empty;

        /// <summary>
        /// 发布次数
        /// </summary>
        public long PublishedCount { get; set; }

        /// <summary>
        /// 处理次数
        /// </summary>
        public long ProcessedCount { get; set; }

        /// <summary>
        /// 成功次数
        /// </summary>
        public long SuccessCount { get; set; }

        /// <summary>
        /// 失败次数
        /// </summary>
        public long FailureCount { get; set; }

        /// <summary>
        /// 重试次数
        /// </summary>
        public long RetryCount { get; set; }

        /// <summary>
        /// 平均处理时间（毫秒）
        /// </summary>
        public double AverageProcessingTimeMs { get; set; }

        /// <summary>
        /// 最小处理时间（毫秒）
        /// </summary>
        public double MinProcessingTimeMs { get; set; } = double.MaxValue;

        /// <summary>
        /// 最大处理时间（毫秒）
        /// </summary>
        public double MaxProcessingTimeMs { get; set; }

        /// <summary>
        /// 当前活跃数
        /// </summary>
        public long CurrentActive { get; set; }

        /// <summary>
        /// 成功率
        /// </summary>
        public double SuccessRate => ProcessedCount > 0 ? (double)SuccessCount / ProcessedCount : 0;

        /// <summary>
        /// 本地事件数量
        /// </summary>
        public long LocalEventCount { get; set; }

        /// <summary>
        /// 分布式事件数量
        /// </summary>
        public long DistributedEventCount { get; set; }

        /// <summary>
        /// 最后处理时间
        /// </summary>
        public System.DateTime? LastProcessedTime { get; set; }
    }
}